/*
角色数值计算
*/
window._ROLEDATA = {

    //血量成长比例
    hpper:8,

    //属性上限
    maxAttrConfig:{
    },

    //静止随机属性
    forbiddenRandomAttr:{
        attack:1,//攻击
        defend:1,//防御
        mattack:1,//法术攻击
        mdefend:1,//法术防御
        maxHp:1,//血量上限
        maxMagic:1,//魔法上限
        speed:1,//速度
        beginEnergy:1
    },

    //值为1的属性
    oneAttrConfig:{

        lianji:1,//物理连击数
        xiezhan:1,//协战
        fanji:1,//反击
        lianjiSkill:1,//法术连击最高1
        xiezhanSkill:1,//法术共鸣
        fanjiSkill:1,//法术反击
		dun:1,//嘲讽
        beActive:1,//激活
        strongEnergy:1,//不朽？
        magicCloth:1,//魔法护甲
        shendun:1,//圣盾
        lianfa:1//连发

    },

    //主属性
    _mainAttrConfig:{
        attack:1,//攻击
        defend:1,//防御
        mattack:1,//法术攻击
        mdefend:1,//法术防御
        maxHp:1,//血量上限
        maxMagic:1,//魔法上限
    },

    //加成系属性
    _perAttrConfig:{
        attackPer:'attack',//攻击
        defendPer:'defend',//防御
        mattackPer:'mattack',//法术攻击
        mdefendPer:'mdefend',//法术防御
        maxHpPer:'maxHp',//血量上限
        maxMagicPer:'maxMagic',//魔法上限
    },

    //咕咕鸡固定属性
    suoxiaoAttr:{
        maxHp:50,//血量上限
        maxMagic:1000,//魔法上限
        gushang:50,
        speed:30
    },


    //所有属性
    _defaultObj:{
        //主属性
        attack:0,//攻击
        defend:0,//防御
        mattack:0,//法术攻击
        mdefend:0,//法术防御
        maxHp:0,//血量上限
        maxMagic:0,//魔法上限

        //主属性比例
        attackPer:0,//攻击
        defendPer:0,//防御
        mattackPer:0,//法术攻击
        mdefendPer:0,//法术防御
        maxHpPer:0,//血量上限
        maxMagicPer:0,//魔法上限

        //副属性
        speed:0,//速度
        move:0,//移动
        distance:1,//射程
        blood:0,//吸血
        sblood:0,//嗜血
        gedang:0,//格挡

        hit:0,//命中
        miss:5,//闪避
        damagePlus:0,//增伤
        damageReduce:0,//减伤
        superAttack:5,//暴击概率
        superAttackMiss:0,//暴击防御
        superDamagePlus:0,//暴击增伤
        superDamageReduce:0,//暴击减伤


        defendReduce:0,//防御减免
        defendReduceMiss:0,//防御减免免疫
        mdefendReduce:0,//法术防御减免
        mdefendReduceMiss:0,//法术防御减免免疫


        stun:0,//击晕概率
        stunMiss:0,//击晕免疫
        attackBack:0,//物理反伤
        mattackBack:0,//法术反伤
        hpRecovery:0,//回血
        MagicRecovery:1,//回蓝
        firePlus:0,//火强
        fireReduce:0,//火抗
        waterPlus:0,//水强
        waterReduce:0,//水抗
        woodPlus:0,//木强
        woodReduce:0,//木抗
        soliPlus:0,//土强化
        soliReduce:0,//土抗
        goldPlus:0,//金强
        goldReduce:0,//金抗

        //战斗特殊
        lianji:0,//物理连击数
        xiezhan:0,//协战
        fanji:0,//反击

        lianjiSkill:0,//法术连击最高1
        xiezhanSkill:0,//法术共鸣
        fanjiSkill:0,//法术反击
        
        jianshe:0,//溅射比例 百分制

        //功能特殊 待补充
		beginEnergy:0,//初始能量
        beginMagic:0,//初始技能灯
		death:0,//致命
		dun:0,//嘲讽
        beActive:0,//激活
        strongEnergy:0,//不朽？
        magicCloth:0,//魔法护甲
        goldCloth:0,//金钱护甲
        shendun:0,//圣盾
        lianfa:0,//连发
        gushang:0,//固伤
        clearBody:0,//净体
        suoxiao:0,//咕咕鸡
        anqimianyi:0,//暗器免疫
        hunSkin:0,//反震
        weifeng:0,//威风
        poge:0,//破格
        yuanhun:0,//怨魂
        againMove:0,//再移动
        scope:0,//范围

        //障碍技能
        pandeng:0,//攀登
        caoduo:0,//操舵
        youji:0,//游击
        bidu:0,//避毒
        bihuo:0,//避火
        fly:0,//飞天

        //体魄
        hardwork:0,//经验比例
        shareExp:0,//共享经验
        shareXw:0,//共享修为
        nodeath:0,//致命免疫

        //偷盗
        itemDrop:0,//丹药掉落
        equipDrop:0,//装备掉落
        fitemDrop:0,//暗器&红蓝掉落
        skillDrop:0,//技能掉落
        dropLv:0,//掉落等级--对应商店等级

        invent:0,//发明--随机召唤机械

        missEnergy:0,//蓄势 闪避加能量
        hitEnergy:0,//集气  挨打加能量
        statusBody:0,//诅咒之躯 被打给来源附加debuff
        magicPer:0,//技能命中
        fanzhi:0,//繁殖 召唤技能数量为4

        aqPer:0,//暗器命中
        aqDistance:0,//暗器射程
        aqScope:0,//暗器范围
        aqDamage:0,//暗器增伤
        poison:0,//淬毒 普攻附加剧毒

        eye:0,//显形 反隐形
        ironwall:0,//铁壁防止反伤
        clearHeart:0,//清心防止招募

        godson:0,//神子 修为可达9999
    },

    //重新评估角色评级 _ROLEDATA.resetRoleQua(role);
    resetRoleQua:function(role){

        if(role){

            var total = role.plv + role.ilv + role.dlv + role.mlv + role.hlv;
            if(total>=7000){
                role.realqua = 'SS';
            }else if(total>5000){
                role.realqua = 'S';
            }else if(total>3000){
                role.realqua = 'A';
            }else if(total>1000){
                role.realqua = 'B';
            }else if(total>500){
                role.realqua = 'C';
            }
        }

    },

    //获取角色真实评级 _ROLEDATA.getRoleQua(role);
    getRoleQua:function(role){

        if(role){

            if(role.realqua){
                return role.realqua;
            }

            if(_NPC[role.npctype]){
                return _NPC[role.npctype].level;
            }

        }

        return 'D';
    },

    //主属性计算公式
    getBaseAttrByLevel:function(lv,attrlv){
        return Math.floor(50*lv+attrlv/1500*50*lv);
    },

    //主属性魔法计算公式
    getBaseMagicByLevel:function(lv,player){
        //return Math.floor(Math.floor(lv/100+1)*(player.ilv+player.mlv)/2);
        return 5;
    },
	
	getRoleLv:function(player){
		var roleData = _ROLEDATA.getRoleDataOfPlayer(player);
        return roleData.lv;
	},


    attrbuff:function(statusattr,ggjobj){
        if(statusattr!=null){
            for(var status in statusattr){
                if(ggjobj[status]){
                    ggjobj[status]=Math.floor(ggjobj[status]*(1+statusattr[status]));
                    if(ggjobj[status]<=0){
                        ggjobj[status]=0;
                    }
                }
            }
        }
    },

    getXwXz:function(player){
        var xwxzobj = {};
        if(player.xw){
            for(var key in player.xw){
                if(_XW[key]){
                    var per = player.xw[key]/_CS.maxxw;
                    var attr = _XW[key].attr;
                    if(!xwxzobj[attr]){
                        xwxzobj[attr]=0;
                    }

                    xwxzobj[attr]+=per;
                }
            }
        }
        return xwxzobj;
    },

    //计算角色真实属性
    getRoleDataOfPlayer(player){

        var lv = _EXPMANAGER.getLevelByExp(player.exp);
        var tmpobj = _CU.cloneObj(_ROLEDATA._defaultObj);

        tmpobj.lv = lv;
        tmpobj.realjob = _MRDL.getJobOfRole(player);
        var jobConfig = _JOB[tmpobj.realjob];
        tmpobj.ftype = player.ftype;

        if(tmpobj.ftype=="火"){
            tmpobj.bihuo=1;
        }else if(tmpobj.ftype=="水"){
            tmpobj.caoduo=1;
        }else if(tmpobj.ftype=="金"){
            tmpobj.pandeng=1;
        }else if(tmpobj.ftype=="土"){
            tmpobj.bidu=1;
        }else if(tmpobj.ftype=="木"){
            tmpobj.youji=1;
        }

        tmpobj.attack=_ROLEDATA.getBaseAttrByLevel(lv,player.plv);
        tmpobj.defend=_ROLEDATA.getBaseAttrByLevel(lv,player.dlv);
        tmpobj.mattack=_ROLEDATA.getBaseAttrByLevel(lv,player.ilv);
        tmpobj.mdefend=_ROLEDATA.getBaseAttrByLevel(lv,player.mlv);
        tmpobj.maxHp=_ROLEDATA.getBaseAttrByLevel(lv,player.hlv)*_ROLEDATA.hpper;
        tmpobj.maxMagic= _ROLEDATA.getBaseMagicByLevel(lv,player);

        var jobattr = _ROLEDATA.getAttrFromJob(tmpobj.realjob);
        var equipattr = _ROLEDATA.getAttrFromEquip(player);
        var skillattr = _ROLEDATA.getAttrFromSkill(player);
        var animalsattr = _ROLEDATA.getAttrFromAnimals(player);
        var chaattr = _CHARACTERHELPER.getAttrOfCha(player.cha);

        tmpobj = _ROLEDATA.attrCombine(tmpobj,jobattr);
        if(!_STATUSHELPERCHESS.hasStatus(player,'jiao')){
            tmpobj = _ROLEDATA.attrCombine(tmpobj,equipattr);
        }
        //中了状态 被动失效
        if(!_STATUSHELPERCHESS.hasStatus(player,'feng')){
            tmpobj = _ROLEDATA.attrCombine(tmpobj,skillattr);
            tmpobj = _ROLEDATA.attrCombine(tmpobj,animalsattr);
        }
        tmpobj = _ROLEDATA.attrCombine(tmpobj,chaattr);

        //比例被动
        for(var key in _ROLEDATA._perAttrConfig){
            var perattr = key;
            var tmpattr = _ROLEDATA._perAttrConfig[key];
            if(tmpobj[perattr]){
                tmpobj[tmpattr]=Math.floor(tmpobj[tmpattr]*(1+tmpobj[perattr]/100));
            }
        }

        //职业修正
        tmpobj.attack=Math.floor(tmpobj.attack*jobConfig.xz.attack);
        tmpobj.defend=Math.floor(tmpobj.defend*jobConfig.xz.defend);
        tmpobj.mattack=Math.floor(tmpobj.mattack*jobConfig.xz.mattack);
        tmpobj.mdefend=Math.floor(tmpobj.mdefend*jobConfig.xz.mdefend);
        tmpobj.maxHp=Math.floor(tmpobj.maxHp*jobConfig.xz.maxHp);

        //修为修正
        var xwxzobj = _ROLEDATA.getXwXz(player);
        for(var key in xwxzobj){
            if(tmpobj[key]){
                tmpobj[key]=Math.floor(tmpobj[key]*(1+xwxzobj[key]));
            }
        }

        for(var key in tmpobj){
            if(tmpobj[key]<0){
                tmpobj[key]=0;
            }
            //判定是否存在上限
            if(_ROLEDATA.maxAttrConfig[key]
                &&tmpobj[key]>_ROLEDATA.maxAttrConfig[key]){
                tmpobj[key]=_ROLEDATA.maxAttrConfig[key];
            }
        }

        _STATUSHELPERCHESS.resetRoleDataByStatus(player,tmpobj);

        if(!tmpobj.maxHp||tmpobj.maxHp<=0){
            tmpobj.maxHp=1;
        }

        return tmpobj;
    },

    //获取角色可用技能
    getSkillOfplayer:function(player){
        /*
        if(player.animals&&_ANIMALS[player.animals]&&!_ANIMALS[player.animals].noskill){
            return _ANIMALS[player.animals].skill;
        }else{
            return player.skill;
        }*/

        return player.skill;
    },

    //计算变身后附加属性
    getAttrFromAnimals:function(player){
        if(player.animals&&_ANIMALS[player.animals]){
            return _ANIMALS[player.animals].attr;
        }

        return null;
    },


    //计算职业附加属性
    getAttrFromJob:function(job){
        if(job&&_JOB[job])
            return _JOB[job].attr;
        return null;
    },

    //计算装备附加属性
    getAttrFromEquip:function(player){
        if(!player.equip){
            return null;
        }
        var tempattr = {};
        for(var i=0;i<player.equip.length;i++){
            if(player.equip[i]){
                //var equipattr = player.equip[key].attr;
                var equipattr = _ROLEDATA.getAttrOfEquip(player.equip[i]);
                tempattr = _ROLEDATA.attrCombine(tempattr,equipattr);
            }
        }
        return tempattr;
    },

    getAttrOfEquip:function(equipInfo){

        if(!equipInfo){
            return ;
        }

        var tempattr = _CU.cloneObj(equipInfo.attr);
        if(equipInfo.stone){
            for(var i=0;i<equipInfo.stone.length;i++){

                if(equipInfo.stone[i].value){

                    var stoneConfig = _M[equipInfo.stone[i].value];

                    if(stoneConfig){

                        tempattr = _ROLEDATA.attrCombine(tempattr,stoneConfig.attr);

                    }

                }

            }
        }

        return tempattr;

    },

    //计算技能附加属性
    getAttrFromSkill:function(player){
        if(!player.skill)
            return null;
        var tempattr = {};
        for(var i=0;i<player.skill.length;i++){
            if(player.skill[i]&&_SKILLBD[player.skill[i]]){
                var skillbdattr = _SKILLBD[player.skill[i]].attr;
                tempattr = _ROLEDATA.attrCombine(tempattr,skillbdattr);
            }
        }
        return  tempattr;
    },

    //计算状态附加属性
    getAttrFromStatus:function(player){
        if(player.statusArr){
            var tempattr={};
            for(var i=0;i<player.statusArr.length;i++){
                var statusName = player.statusArr[i].name;
                var nmb = player.statusArr[i].nmb;
                if(window._STATUSCONF[statusName]){
                    tempattr[window._STATUSCONF[statusName].attr]
                    =window._STATUSCONF[statusName].flag?_CS.buffper:-_CS.buffper;
                }
            }
            return tempattr;
        }

        return null;
    },

    //获取角色单一真实属性
    getRealAttr:function(player,attr){

        var realdata = _ROLEDATA.getRoleDataOfPlayer(player);

        return realdata[attr];
        
    },

    //属性合并
    attrCombine:function(oldattr,newattr){

        if(!oldattr){
            return _CU.cloneObj(newattr);
        }else{
            if(!newattr){
                return oldattr;
            }else{

                for(var key in newattr){

                    if(oldattr[key]){
                        oldattr[key]+=newattr[key];
                    }else{
                        oldattr[key]=newattr[key];
                    }

                }

            }
        }

        return oldattr;

    },


    //获得属性文字
    getAttrString:function(isMain,data){

        var result = "";

        if(isMain){

            for(var key in _ROLEDATA._mainAttrConfig){

                if(data[key]!=0&&_DICT[key]){
                    var name = _DICT[key];

                    result+=name+":"+data[key]+"\n";
                }

            }


        }else{

            for(var key in data){

                if(
                    !_ROLEDATA._mainAttrConfig[key]
                    &&key!='ftype'
                    &&data[key]!=0
                    &&_DICT[key]
                ){

                    var name = _DICT[key];

                    result+=name+":"+data[key]+"\n";

                }

            }

        }

        return result;
    },


    getRoleXwLevel:function(player){
        if(!player.xwlevel){
            if(_NPC[player.npctype]){
                player.xwlevel = _CU.cloneObj(_NPC[player.npctype].npcxw);
            }
        }

        return player.xwlevel;
    }
}